package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：zhao
 * @date ：Created in 2020/1/2 0002
 * @description ：权限持久层
 * @version: 1.0
 */
@Slf4j
@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Permission> page = permissionDao.selectByConditon(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Permission permission) {
        permissionDao.insert(permission);

    }

    @Override
    public void delete(Integer id) {
        //校验是否可以删除
        Long count = permissionDao.countRoleByPermissionId(id);
        if(count >0){
            throw new BusinessRuntimeException("当前权限有相关角色信息，不能删除");
        }
        //实际删除操作
        permissionDao.deleteById(id);

    }

    @Override
    public void edit(Permission permission) {
        permissionDao.update(permission);

    }

    @Override
    public Permission findById(Integer id) {
        return permissionDao.selectById(id);
    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.selectAll();
    }

}
